﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Data.OleDb
Public Class Principal
    Inherits KryptonForm

    Const WM_SYSCOMMAND As Int32 = &H112
    Const SC_MINIMIZE As Int32 = &HF020
    Const SC_RESTORE As Int32 = &HF120
    Const SC_CLOSE As Int32 = &HF060

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        Me.TextExtra = Config.getVersao() 'Texto que fica próximo aos botões de fechar/minimizar

        flashPanel.BGColor = "E3E6E4" 'Fundo do painel flash
        flashPanel.Visible = False

    End Sub

    Private Sub utilBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles utilBackup.Click
        Dim frm As New Backup("normal")
        frm.ShowDialog()

    End Sub

    Private Sub arqSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles arqSair.Click
        If (KryptonMessageBox.Show("Deseja realmente sair?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            End
        End If
    End Sub

    Private Sub flashPanel_FSCommand(ByVal sender As System.Object, ByVal e As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles flashPanel.FSCommand
        Select Case (e.command)
            Case "consultaCliente"
                conCli.PerformClick()
            Case "consultaProduto"
                conProd.PerformClick()
            Case "consultaVeiculo"
                conAuto.PerformClick()
            Case "orcamento"
                orcNovo.PerformClick()
            Case "calculadora"
                Process.Start("calc")
            Case "internet"
                Process.Start("https://www.google.com.br")
            Case "backup"
                utilBackup.PerformClick()
            Case "Ajuda"
                ajudaTopic.PerformClick()

            Case "sair"
                If (KryptonMessageBox.Show("Deseja realmente sair?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                    End
                End If

        End Select
    End Sub

#Region "Relatorios"
    Private Sub relatCadCliFis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadCliFis.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT * FROM tblCliente WHERE tipoCliente = 'fisico'", "tblCliente", Config.DiretorioAplicacao & "/../../../Relatorio/relatorioClienteFisico.rpt")
    End Sub

    Private Sub relatCadCliJur_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadCliJur.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT * FROM tblCliente WHERE tipoCliente = 'juridico'", "tblCliente", Config.DiretorioAplicacao & "/../../../Relatorio/relatorioClienteJuridico.rpt")
    End Sub

    Private Sub relatCadProd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadProd.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT tblProduto.*,tblFornecedor.nomeFornecedor, tblMarcaProduto.descricaomarcaProduto FROM ((tblProduto INNER JOIN tblMarcaProduto ON tblMarcaProduto.codmarcaProduto = tblProduto.codmarcaProduto) INNER JOIN tblFornecedor ON tblFornecedor.codFornecedor = tblProduto.codFornecedor)" _
                                             , "tblProduto" _
                                             , Config.DiretorioAplicacao & "/../../../Relatorio/relatorioProduto.rpt")
    End Sub

    Private Sub relatCadAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadAuto.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT tblAuto.*,tblModeloAuto.descricaomodeloAuto,tblMontadoraAuto.descricaomontadoraAuto,tblCliente.nomeCliente,tblCorAuto.descricaocorAuto,tblCombustivel.descricaoCombustivel FROM (((((tblAuto INNER JOIN tblModeloAuto ON tblModeloAuto.codmodeloAuto = tblAuto.codmodeloAuto) INNER JOIN tblMontadoraAuto ON tblMontadoraAuto.codmontadoraAuto = tblAuto.codmontadoraAuto) INNER JOIN tblCliente ON tblCliente.codCliente = tblAuto.codCliente) INNER JOIN tblCorAuto ON tblCorAuto.codCorAuto = tblAuto.codCorAuto)INNER JOIN tblCombustivel ON tblCombustivel.codCombustivel = tblAuto.codCombustivel)" _
                                             , "tblAuto" _
                                             , Config.DiretorioAplicacao & "/../../../Relatorio/relatorioAutomovel.rpt")
    End Sub

    Private Sub relatCadFunc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadFunc.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT * FROM tblFuncionario", "tblFuncionario", Config.DiretorioAplicacao & "/../../../Relatorio/relatorioFuncionario.rpt")
    End Sub

    Private Sub relatCadForn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles relatCadForn.Click
        VisualizadorRelatorio.gerarRelatorio("SELECT * FROM tblFornecedor", "tblFornecedor", Config.DiretorioAplicacao & "/../../../Relatorio/relatorioFornecedor.rpt")
    End Sub
#End Region

#Region "Consulta"

    Private Sub conCli_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles conCli.Click
        Dim frm As New ConsultaCliente()
        frm.ShowDialog()
    End Sub

    Private Sub conFunc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles conFunc.Click
        Dim frm As New ConsultaFuncionario()
        frm.ShowDialog()
    End Sub

    Private Sub conForn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles conForn.Click
        Dim frm As New ConsultaFornecedor()
        frm.ShowDialog()

    End Sub

    Private Sub conAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles conAuto.Click
        Dim frm As New ConsultaAutomovel()
        frm.ShowDialog()

    End Sub

    Private Sub conProd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles conProd.Click
        Dim frm As New ConsultaProduto()
        frm.ShowDialog()
    End Sub
#End Region

#Region "Cadastro"
    Private Sub cadAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadAuto.Click
        Dim frm As New ConsultaAutomovel()
        frm.ShowDialog()

    End Sub

    Private Sub cadFunc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadFunc.Click
        Dim frm As New ConsultaFuncionario
        frm.ShowDialog()
    End Sub

    Private Sub cadProd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadProd.Click
        Dim frm As New ConsultaProduto()
        frm.ShowDialog()
    End Sub

    Private Sub cadCli_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadCli.Click
        Dim frm As New ConsultaCliente()
        frm.ShowDialog()
    End Sub

    Private Sub cadForn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadForn.Click
        Dim frm As New ConsultaFornecedor()
        frm.ShowDialog()

    End Sub

    Private Sub cadUsu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cadUsu.Click
        Dim frm As New CadastroUsuario()
        frm.ShowDialog()
    End Sub

    Private Sub FluxoDeCaixaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FluxoDeCaixaToolStripMenuItem.Click
        Dim frm As New CaixaRegistradora()
        frm.ShowDialog()
    End Sub

#End Region

    Private Sub usuLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usuLogin.Click
        Dim frm As New Login()
        Dim resultado = frm.ShowDialog()

        If (resultado = Windows.Forms.DialogResult.OK) Then
            usuario.Text = "Usuário: " & Login.usuario.Nome
            If (Login.usuario.Nivel = 1) Then
                ativarMenuAdmin()

            ElseIf (Login.usuario.Nivel = 2) Then
                ativarMenuComum()
            End If

            'Associa teclas de atalho
            usuLogin.ShortcutKeys = Keys.None
            usuSair.ShortcutKeys = Keys.Control + Keys.L
        ElseIf (resultado = Windows.Forms.DialogResult.None) Then
            usuario.Text = "Usuário: "
        End If

        frm.Dispose()
    End Sub

    Private Sub ajudaSobre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ajudaSobre.Click
        Dim frm As New Sobre()
        frm.ShowDialog()
    End Sub

    Private Sub orcNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles orcNovo.Click
        Dim frm As New Orcamento()
        frm.ShowDialog()
    End Sub

    Private Sub notificacao_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles icone.MouseDoubleClick
        Me.ShowInTaskbar = True
        Me.Show()
        icone.Visible = False
        Me.Focus()
    End Sub

    Private Sub icone_BalloonTipClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icone.BalloonTipClicked
        Me.ShowInTaskbar = True
        Me.Show()
        icone.Visible = False
        Me.Focus()
    End Sub

    Private Sub usuSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usuSair.Click
        Dim resp = KryptonMessageBox.Show("Deseja encerrar sua sessão?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If (resp = Windows.Forms.DialogResult.Yes) Then
            Login.usuario = Nothing
            usuario.Text = "Usuário: Nenhum usuário está logado"
            desativarMenu()

            'Associa teclas de atalho
            usuSair.ShortcutKeys = Keys.None
            usuLogin.ShortcutKeys = Keys.Control + Keys.L
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_SYSCOMMAND Then
            If m.WParam.ToInt32 = SC_MINIMIZE Then
                'User clicked "minimize"
                Me.Hide()
                Me.ShowInTaskbar = False
                icone.Visible = True
                icone.BalloonTipIcon = ToolTipIcon.Info
                icone.BalloonTipTitle = "O Wotan foi minimizado"
                icone.BalloonTipText = "Quando quiser utilizá-lo, dê um duplo clique no ícone do relógio ou clique neste balão."
                icone.ShowBalloonTip(5)
            ElseIf m.WParam.ToInt32 = SC_RESTORE Then
                'Restoring

            ElseIf (m.WParam = SC_CLOSE) Then
                'Se o usuário clicar no botão de fechar
                End
            End If
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub desativarMenu()

        'Menu do usuário
        usuSair.Visible = False
        usuLogin.Visible = True

        'Outros menus
        cad.Visible = False
        con.Visible = False
        orc.Visible = False
        os.Visible = False
        cxReg.Visible = False
        relat.Visible = False
        util.Visible = False

        usuConAdm.Visible = False
        usuminhaConta.Visible = False

        'Painel em flash
        flashPanel.Movie = Config.DiretorioPainelFlash
        flashPanel.Visible = False
    End Sub

    Private Sub ativarMenuAdmin()
        desativarMenu()

        'Menu do usuário
        usuSair.Visible = True
        usuLogin.Visible = False
        usuminhaConta.Visible = True
        'Outros menus
        cad.Visible = True
        con.Visible = True
        orc.Visible = True
        os.Visible = True
        cxReg.Visible = True
        relat.Visible = True
        util.Visible = True

        cadUsu.Visible = True
        usuConAdm.Visible = True
        usuminhaConta.Visible = True

        'Painel em flash
        flashPanel.Movie = Config.DiretorioPainelFlash
        flashPanel.Visible = True
    End Sub

    Private Sub ativarMenuComum()
        desativarMenu()

        'Menu do usuário
        usuSair.Visible = True
        usuLogin.Visible = False
        usuminhaConta.Visible = True

        'Outros menus
        cad.Visible = True
        con.Visible = True
        orc.Visible = True
        os.Visible = True
        cxReg.Visible = False
        relat.Visible = True
        util.Visible = True

        cadUsu.Visible = False
        usuConAdm.Visible = False
        usuminhaConta.Visible = True

        'Painel em flash
        flashPanel.Movie = Config.DiretorioPainelFlash
        flashPanel.Visible = True
    End Sub

    Private Sub osNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As New Orcamento()
        frm.ShowDialog()
    End Sub

    Private Sub FuncionárioToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FuncionárioToolStripMenuItem.Click
        RelAnaliticoFunc.ShowDialog()
    End Sub

    Private Sub ProdutoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProdutoToolStripMenuItem.Click
        RelAnaliticoProduto.ShowDialog()
    End Sub

    Private Sub AutomóvelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AutomóvelToolStripMenuItem.Click
        RelAnaliticoAuto.ShowDialog()
    End Sub

    Private Sub orcCon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles orcCon.Click
        Dim frm As New ConsultaOrcamento()
        frm.ShowDialog()
    End Sub

    Private Sub osCon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles osCon.Click
        Dim frm As New ConsultaOS()
        frm.ShowDialog()
    End Sub

    Private Sub ajudaTopic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ajudaTopic.Click
        Process.Start(Config.DiretorioAplicacao & "/../../../Resources/help_wotan.chm")
    End Sub

    Private Sub MinhaContaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usuminhaConta.Click
        Dim frm As New ConsultaUsuario()
        frm.ShowDialog()
    End Sub

    Private Sub ConsultarUsuáriosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usuConAdm.Click
        Dim frm As New consultaUsuarioadm()
        frm.ShowDialog()
    End Sub

    Private Sub ConsultaAvançadaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConsultaAvançadaToolStripMenuItem.Click
        Dim frm As New consultaCaixa()
        frm.ShowDialog()
    End Sub

    Private Sub GráficoDeOSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GráficoDeOSToolStripMenuItem.Click
        Dim frm As New GraficoOS()
        frm.ShowDialog()
    End Sub

    Private Sub Principal_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            Dim paths() As String = e.Data.GetData(DataFormats.FileDrop)
            If paths.Length > 1 Then
                Exit Sub
            ElseIf (paths.Length = 1) Then
                Dim fi As New IO.FileInfo(paths(0))
                If (fi.Extension = ".WSB") Then
                    If (KryptonMessageBox.Show("Deseja realmente efetuar o backup?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                        IO.File.Copy(paths(0), Config.DiretorioBD, True)
                        KryptonMessageBox.Show("Backup efetuado com sucesso.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Else
                    KryptonMessageBox.Show("Este não é um arquivo de backup válido.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        End If
    End Sub

    Private Sub Principal_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
        If (e.Data.GetDataPresent(DataFormats.FileDrop) And Login.usuario IsNot Nothing) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
End Class